A Binary File


Binary File

It is possible to store any data type variable in a file. However, it is important to read the data in the same order it was written.
Es posible almacenar una variable de cualquier tipo de datos en un archivo. Sin embargo, es importante leer los datos en el mismo orden en el que fueron escritos.

Problem 1
Write a program called SaveNumb to create and read a binary file with a list of numbers.
Escriba un programa llamado SaveNumb para crear y leer un archivo binario con una lista de números.

SaveNumb

SaveNumb.cpp
void SaveNumb::Window_Open(Win::Event& e)
{
}

void SaveNumb::btOpen_Click(Win::Event& e)
{
     Win::FileDlg dlg;
     dlg.SetFilter(L"Numeric Documents (*.dat)\0*.dat\0\0", 0, L"dat");
     if (dlg.BeginDialog(hWnd, L"Open a numeric document", false) == true)
     {
          //__________________________________ Open the file
          Sys::File file;
          if (file.CreateForReading(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Open");
               return;
          }
          //__________________________________ Read the array size
          int count = 0;
          file.Read(count);
          //__________________________________ Read the array elements
          valarray<double> data(count);
          for(int i = 0; i<count; i++) file.Read(data[i]);
          //__________________________________ Display the array
          wstring text;
          Sys::Convert::ToString(data, text);
          this->tbxInput.Text = text;
     }
}

void SaveNumb::btSave_Click(Win::Event& e)
{
     //____________________________________ Extract the array from the textbox
     valarray<double> data;
     Sys::Convert::ToVector(tbxInput.Text, data);

     //____________________________________ Get the filename
     Win::FileDlg dlg;
     dlg.SetFilter(L"Numeric Documents (*.dat)\0*.dat\0\0", 0, L"dat");
     if (dlg.BeginDialog(hWnd, L"Save a numeric document", true) == true)
     {
          //__________________________________ Create the file
          Sys::File file;
          if (file.CreateForWritting(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Save");
               return;
          }
          //__________________________________ Write the array size
          const int count = data.size();
          file.Write(count);
          //__________________________________ Write the array elements
          for(int i = 0; i < count; i++) file.Write(data[i]);
     }
}

Problem 2
Write a program called PointList to create and read a binary file with a list of 3D points. In this case, each point has the same size (number of bytes: 3*sizeof(double)), and each point can be easily read and written in the file.
Escriba un programa llamado PointList para crear y leer un archivo binario con una lista de puntos 3D. En este caso, cada punto tiene el mismo tamaño (número de bytes: 3*sizeof(double)), y cada punto puede ser fácilmente leído y escrito en el archivo.

PointList1

PointList2

PointList3

PointList.h
#pragma once //______________________________________ PointList.h
#include "resource.h"
class PointList: public Win::Dialog
{
public:
     PointList()
     {
     }
     ~PointList()
     {
     }
     struct Point3D
     {
          double x;
          double y;
          double z;
     };
protected:
     ...
};

PointList.cpp
void PointList::Window_Open(Win::Event& e)
{
}

void PointList::btOpen_Click(Win::Event& e)
{
     Win::FileDlg dlg;
     dlg.SetFilter(L"3D Point Documents (*.pot)\0*.pot\0\0", 0, L"pot");
     if (dlg.BeginDialog(hWnd, L"Open a 3D point document", false) == true)
     {
          //__________________________________ Open the file
          Sys::File file;
          if (file.CreateForReading(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Open");
               return;
          }
          //__________________________________ Read the vector size
          int count = 0;
          file.Read(count);
          //__________________________________ Read the array elements
          vector<Point3D> data(count);
          DWORD bytesToRead = sizeof(Point3D);
          for(int i = 0; i<count; i++)
          {
               file.Read(&data[i], bytesToRead);
          }
          //__________________________________ Display the array
          wstring text;
          wstring buffer;
          for(int i = 0; i<count; i++)
          {
               Sys::Format(text, L"%g, %g, %g\r\n", data[i].x, data[i].y, data[i].z);
               buffer += text;
          }
          this->MessageBox(buffer, L"Points", MB_OK | MB_ICONINFORMATION);
     }
}

void PointList::btSave_Click(Win::Event& e)
{
     //____________________________________ Create a list of Points
     vector<Point3D> data(2);
     data[0].x = 1.0;
     data[0].y = 2.0;
     data[0].z = 3.0;
     //
     data[1].x = 10.0;
     data[1].y = 11.0;
     data[1].z = 21.0;
     //____________________________________ Get the filename
     Win::FileDlg dlg;
     dlg.SetFilter(L"3D Point Documents (*.pot)\0*.pot\0\0", 0, L"pot");
     if (dlg.BeginDialog(hWnd, L"Save a 3D Point document", true) == true)
     {
          //__________________________________ Create the file
          Sys::File file;
          if (file.CreateForWritting(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Save");
               return;
          }
          //__________________________________ Write the array size
          const int count = data.size();
          file.Write(count);
          //__________________________________ Write the array elements
          DWORD bytesToWrite = sizeof(Point3D);
          for(int i = 0; i < count; i++)
          {
               file.Write(&data[i], bytesToWrite);
          }
     }
}

Problem 3
Write a program called Contacts to create and read a binary file with a list of personal information. In this case, each contact has not the same size (due to the use of wstring), and each part of the contact must be read or written separately.
Escriba un programa llamado Contacts para crear y leer un archivo binario con una lista de información personal. En este caso, cada contacto no tiene el mismo tamaño (debido al uso de wstring), y cada parte del contacto debe ser leída y escrita por separado.

Contacts1

Contacts2

Contacts3

Contacts.h
#pragma once //______________________________________ Contacts.h
#include "resource.h"

class Contacts: public Win::Dialog
{
public:
     Contacts()
     {
     }
     ~Contacts()
     {
     }
     struct PersonalInfo
     {
          wstring name;
          int age;
          Sys::Time birthday;
     };
protected:
     ...
};

Contacts.cpp
void Contacts::Window_Open(Win::Event& e)
{
}

void Contacts::btOpen_Click(Win::Event& e)
{
     Win::FileDlg dlg;
     dlg.SetFilter(L"Contacts Documents (*.con)\0*.con\0\0", 0, L"con");
     if (dlg.BeginDialog(hWnd, L"Contacts document", false) == true)
     {
          //__________________________________ Open the file
          Sys::File file;
          if (file.CreateForReading(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Open");
               return;
          }
          //__________________________________ Read the vector size
          int count = 0;
          file.Read(count);
          //__________________________________ Read the vector elements
          vector<PersonalInfo> contacts(count);

          for(int i = 0; i<count; i++)
          {
               file.Read(contacts[i].name);
               file.Read(contacts[i].age);
               file.Read(contacts[i].birthday);
          }
          //__________________________________ Display the contacts
          wstring text;
          wstring buffer;
          for(int i = 0; i<count; i++)
          {
               Sys::Format(text, L"%s, %d, %02d/%02d/%d\r\n",
                    contacts[i].name.c_str(), contacts[i].age,
                    contacts[i].birthday.wDay, contacts[i].birthday.wMonth, contacts[i].birthday.wYear);
               buffer += text;
          }
          this->MessageBox(buffer, L"Contacts", MB_OK | MB_ICONINFORMATION);
     }
}

void Contacts::btSave_Click(Win::Event& e)
{
     //____________________________________ Create a list of Points
     vector<PersonalInfo> contacts;
     PersonalInfo pi;
     //
     pi.name = L"John";
     pi.age = 0;
     pi.birthday = Sys::Time::Now();
     contacts.push_back(pi);
     //
     pi.name = L"Mary";
     pi.age = 15;
     pi.birthday = Sys::Time::Now().AddHours(-15*24*365);
     contacts.push_back(pi);
     //
     //____________________________________ Get the filename
     Win::FileDlg dlg;
     dlg.SetFilter(L"Contacts Documents (*.con)\0*.con\0\0", 0, L"con");
     if (dlg.BeginDialog(hWnd, L"Save a Contacts document", true) == true)
     {
          //__________________________________ Create the file
          Sys::File file;
          if (file.CreateForWritting(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Save");
               return;
          }
          //__________________________________ Write the vector size
          const int count = contacts.size();
          file.Write(count);
          //__________________________________ Write each element in the vector
          for(int i = 0; i < count; i++)
          {
               file.Write(contacts[i].name);
               file.Write(contacts[i].age);
               file.Write(contacts[i].birthday);
          }
     }
}

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home